package com.viking.exception;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.viking.domain.BlockExceptResult;


public class MyBlockHandler {
    /**
     * 参数必须包含被流控方法所有参数，且必须有BlockException参数，且返回结果也必须一致
     */
    public static BlockExceptResult myBlockHandler(Integer id,BlockException e) {
        BlockExceptResult r = null;
        if (e instanceof FlowException) {
            r = BlockExceptResult.error(100, "接口限流了");

        } else if (e instanceof DegradeException) {
            r = BlockExceptResult.error(101, "服务降级了");

        } else if (e instanceof ParamFlowException) {
            r = BlockExceptResult.error(102, "热点参数限流了");

        } else if (e instanceof SystemBlockException) {
            r = BlockExceptResult.error(103, "触发系统保护规则了");

        } else if (e instanceof AuthorityException) {
            r = BlockExceptResult.error(104, "授权规则不通过");
        }
        return r;
    }
}
